#!/bin/bash

# Slackware build script for sonic-visualiser

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20230112 bkw: update for 4.5.1.
# 20211207 bkw: update for 4.4.
# - liblo is now a hard dep.
# - jack is a hard dep again.
# - get rid of 10+ year outdated Debian man pages, write an up-to-date one.
#   unfortunately I do not speak French, so the new man page is English-only.
# - upstream switched to meson, can no longer build older versions with
#   this script.

# 20201104 bkw: update for 4.2.
# 20191212 bkw:
# - Update for 4.0.1.
# - New deps: libfishsound capnproto opusfile.
# - Removed JACK as a hard dep.
# - Have slack-desc tell the user what optional deps are built-in.
# - Annoying stuff relating to the test suite.
# - If necessary, source the qt5 profile script.
# - Install French man page.
# - Fix icon reference in .desktop.
# - It looks like the segfault-on-exit problem was caused by an outdated
#   version of qt5. It's gone away with qt5-5.9.8.

# 20181217 bkw:
# - Update for 3.2. This was left stale for a long time due to
#   problems with SBo's old qt5, which has finally been updated,
#   thanks to dive.
# - Get rid of static capnproto, no longer needed (thank the ghods).
# - PulseAudio now works, update README accordingly.
# - Yes, I'm aware that it's segfaulting on exit, but it won't dump
#   core (even though I have coredumps enabled) so I can't easily
#   diagnose it.

# 20170312 bkw:
# - update for 3.0.1
# - parallel build seems OK, remove -j1 (again)
# - 3.0.1 requires a bleeding-edge capnproto (newer than the latest
#   release) so build a static one to link with. When capnproto.SlackBuild
#   gets updated past 0.5.3, this stuff can go away. The static capnproto
#   is used whether or not capnproto is installed on the system, and it's
#   not installed as part of the sonic-visualiser package.
# - add note to README about pulse and portaudio failures. I don't know
#   what's wrong yet, but jack-audio-connection-kit remains a hard dep
#   unless/until I figure it out.
# - upstream fixed the .desktop, remove my code that fixed the old one.
# - stop the unit tests from spamming /root

# 20160819 bkw:
# - parallel build is still broken, revert to -j1

# 20160806 bkw:
# - updated to 2.5, which means adding a qt5 dep.
# - fix parallel build.
# - according to INSTALL.txt, jack-audio-connection-kit is optional,
#   so I was going to remove it from REQUIRES. I tried building without it,
#   but the build failed, so it remains a hard dep for now.

# 20140913 bkw:
# - finally updated (from 1.9 to 2.3)
# - add man page
# - use png icon rather than svg
# - add MIME types for desktop integration
# - add realtime capabilities

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=sonic-visualiser
VERSION=${VERSION:-4.5.1}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG $TMP/$PRGNAM
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# 20211207 bkw: upstream switched from autotools to meson/ninja. Also,
# the test suite is no longer run by default, so all the extra code here
# that was needed to make the tests pass, is gone. And, there's now a
# functional 'install' target.
# According to COMPILE_linux.md, mlton is required. But building without
# it works fine and produces a binary that works fine...
mkdir build
cd build
  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \
  meson .. \
    --buildtype=release \
    --infodir=/usr/info \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --localstatedir=/var \
    --mandir=/usr/man \
    --prefix=/usr \
    --sysconfdir=/etc \
    -Dstrip=true
  "${NINJA:=ninja}"
  DESTDIR=$PKG $NINJA install
cd ..

# binaries already stripped, .desktop already installed.

for i in icons/sv-*x*.png; do
  size="$( basename $i | cut -d- -f2 | cut -d. -f1 )"
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  cp -a $i $dir/$PRGNAM.png
done

mkdir -p $PKG/usr/share/pixmaps
ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

# man page written by SlackBuild author.
mkdir -p $PKG/usr/man/man1
gzip -9c < $CWD/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

# define MIME types to associate *.sv and *.svl
mkdir -p $PKG/usr/share/mime/packages
cat $CWD/$PRGNAM.xml > $PKG/usr/share/mime/packages/$PRGNAM.xml

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a CHANGELOG CITATION COPYING README* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

WITH_PA=WITHOUT
objdump -p $PKG/usr/bin/$PRGNAM | grep -q 'NEEDED.*libportaudio' && WITH_PA=WITH

mkdir -p $PKG/install
sed "s,@WITH_PA@,$WITH_PA," $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

# Only add capability stuff if not disabled:
if [ "${SETCAP:-yes}" = "yes" ]; then
  cat $CWD/setcap.sh >> $PKG/install/doinst.sh
  # Only allow execution by audio group
  chown root:audio $PKG/usr/bin/$PRGNAM
  chmod 0750 $PKG/usr/bin/$PRGNAM
fi

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
